﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using PostSharp.Reflection;
using System.Web.Caching;

namespace LiveWebServiceBase
{
    [Serializable]
    public sealed class AttributeCache : LocationInterceptionAspect
    {
        private string cacheKey
        {
            get
            {
                return CurrentAttributeName + "cahched_value";
            }
        }
        private string CurrentAttributeName;

        public override void RuntimeInitialize(LocationInfo locationInfo)
        {
            this.CurrentAttributeName = locationInfo.Name;

            base.RuntimeInitialize(locationInfo);
        }

        public override void OnSetValue(LocationInterceptionArgs args)
        {
            if (System.Web.HttpContext.Current.Cache[cacheKey] == null)
            {
                System.Web.HttpContext.Current.Cache.Insert(cacheKey, args.Value,
                     new CacheDependency(new string[] 
                     { 
                         Settings.Paths.PathToELMSConfigXML,
                         Settings.Paths.PathToEWSConfigXML,
                         Settings.Paths.PathToPSConfigXML, 
                         Settings.Paths.PathToSSOConfigXML 
                     }),
                     Cache.NoAbsoluteExpiration,
                     TimeSpan.FromHours(24),
                     CacheItemPriority.NotRemovable, null);

            }
            else
            {
                args.Value = System.Web.HttpContext.Current.Cache[cacheKey];
            }
        }

        public override void OnGetValue(LocationInterceptionArgs args)
        {
            if (System.Web.HttpContext.Current.Cache[cacheKey] == null)
            {
                args.Value = null;
            }
            else
            {
                args.Value = System.Web.HttpContext.Current.Cache[cacheKey];
            }
        }
    }
}
